import sys
input = lambda : sys.stdin.readline().strip()
'''
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
╬╬ ▓▓ ▓▓ ╬╬
╬╬ ▓▓ ▓▓ ╬╬
╬╬ ▓▓█████▓▓ ╬╬
╬╬ ▓▓ ▓▓ ╬╬
╬╬ ▓▓ ▓▓ ╬╬
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
'''
for i in range(int(input())):
n = input()
x = len(n)
y = (x-1)*9
new_num = int("1"*x)
z = int(n)//new_num
print(z+y)
#include<bits/stdc++.h>
using namespace std;
void solve(){
long long n;
cin>>n;
long long num=n;
int cnt=0;
long long dig=0;
while(n>0){
dig=n%10;
n=n/10;
cnt++;
}
long long digit=dig;
long long ans=0;
if(cnt==1){
cout<<dig<<endl;
return;
}
int count=cnt;
while(cnt>1){
dig=dig*10+digit;
cnt--;
}
if(num>=dig ){
ans=9*(count-1)+digit;
cout<<ans<<endl;
return;
}
else {
ans=9*(count-1)+digit-1;
cout<<ans<<endl;
return;
}
}
int main(){
int t;
cin>>t;
while(t--){
solve();
}
}
96A - Football | 702B - Powers of Two |
1036A - Function Height | 443A - Anton and Letters |
1478B - Nezzar and Lucky Number | 228A - Is your horseshoe on the other hoof |
122A - Lucky Division | 1611C - Polycarp Recovers the Permutation |
432A - Choosing Teams | 758A - Holiday Of Equality |
1650C - Weight of the System of Nested Segments | 1097A - Gennady and a Card Game |
248A - Cupboards | 1641A - Great Sequence |
1537A - Arithmetic Array | 1370A - Maximum GCD |
149A - Business trip | 34A - Reconnaissance 2 |
59A - Word | 462B - Appleman and Card Game |
1560C - Infinity Table | 1605C - Dominant Character |
1399A - Remove Smallest | 208A - Dubstep |
1581A - CQXYM Count Permutations | 337A - Puzzles |
495A - Digital Counter | 796A - Buying A House |
67A - Partial Teacher | 116A - Tram |